package com.example.forum.auto.tests;


import com.example.forum.auto.common.BrowserName;
import com.example.forum.auto.common.Utils;
import lombok.NonNull;
import lombok.SneakyThrows;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedConditions;

import java.util.UUID;

/**
 * @author hanzishuai
 * Blog :  https://blog.csdn.net/qrwitu142857
 * @date 2025/5/11 22:16
 * @Description
 */
public class RegisterPage extends Utils {
    public static final String url = "http://82.157.200.90:58081/sign-up.html";

    public RegisterPage() {
        super(url);
    }

    public RegisterPage(BrowserName browserName) {
        super(url, browserName);
    }

    /**
     * Created by hanzishuai on 2025/5/11
     *
     * @Description 检查注册页面是否加载完毕
     */
    @SneakyThrows
    public void checkPageRight() {
        driver.findElement(By.cssSelector("#username"));
        driver.findElement(By.cssSelector("#nickname"));
        driver.findElement(By.cssSelector("#password"));
        driver.findElement(By.cssSelector("#passwordRepeat"));
        driver.findElement(By.cssSelector("#app > div:nth-child(6) > label > span"));
    }


    private void register(String username,String nickname,String passwordRepeat, String password,@NonNull Boolean policy) {
        // 将输入框清空
        driver.findElement(By.cssSelector("#username")).clear();
        driver.findElement(By.cssSelector("#nickname")).clear();
        driver.findElement(By.cssSelector("#password")).clear();
        driver.findElement(By.cssSelector("#passwordRepeat")).clear();
        WebElement element = driver.findElement(By.cssSelector("#app > div:nth-child(6) > label > span"));
        if(element.isSelected()) {
            element.click();
        }


        // 输入账号密码，并点击提交
        if (username != null) {
            driver.findElement(By.cssSelector("#username")).sendKeys(username);
        }
        if(nickname != null) {
            driver.findElement(By.cssSelector("#nickname")).sendKeys(nickname);
        }
        if (password != null) {
            driver.findElement(By.cssSelector("#password")).sendKeys(password);
        }
        if (passwordRepeat != null) {
            driver.findElement(By.cssSelector("#passwordRepeat")).sendKeys(passwordRepeat);
        }
        if(policy) {
            WebElement element2 = driver.findElement(By.cssSelector("#app > div:nth-child(6) > label > span"));
            if(element2.isSelected()) {
                element2.click();
            }
        }

        driver.findElement(By.cssSelector("#submit")).click();
    }


    @SneakyThrows
    public void registerFail() {
        // 不输入账号和密码，点击提交
        register(null, null, null, null,false);
        register("test2", null, null, null,false);
        register("test2", "test2", null, null,false);
        register("test2", "test2", "123456", null,false);
        register("test2", "test2", "123456", "123",false);
        register("test2", "test2", "123456", "123456",false);
        register("awa", "awa", "123456", "123456",true);
    }

    @SneakyThrows
    public void registerSuccess() {
        String username = UUID.randomUUID().toString().replace("-", "").substring(0,7);

        register(username, username, "123456", "123456",true);

        // 截图
        screenShot("registerSuccess");

        // 注册成功会跳转到登录页面
        String text1 = driver.findElement(By.cssSelector("#submit")).getText();
        assert  "登录".equals(text1);
    }


    public void intoLoginPage() {
        WebElement element = driver.findElement(By.cssSelector("#app > div.text-center.text-muted.mt-3 > a"));
        wait.until(ExpectedConditions.elementToBeClickable(element));
        element.click();
    }
}
